//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmBlackScholesMultiStrikeMesher.h"
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/Fdm1dMesher.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::CFdmBlackScholesMultiStrikeMesher (UInt64 size, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double maturity, Cephei::Core::IVector<Double>^ strikes, Microsoft::FSharp::Core::FSharpOption<Double>^ eps, Microsoft::FSharp::Core::FSharpOption<Double>^ scaleFactor) : CFdm1dMesher(CFdmBlackScholesMultiStrikeMesher::typeid)
{
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phFdmBlackScholesMultiStrikeMesher = NULL;
#endif
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size); //d
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity); //d
        strikes->Lock();
        INativeVector<Double>^ _NCIstrikes = strikes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCstrikes = safe_cast<CDoubleVector^>(_NCIstrikes);
        std::vector<QuantLib::Real>& _strikes = static_cast<std::vector<QuantLib::Real>&> (_NCstrikes->GetReference ());
        QuantLib::Real _eps = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (eps) ? (QuantLib::Real)ValueHelper::Convert (eps->Value) : 0.0001); //4
        QuantLib::Real _scaleFactor = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (scaleFactor) ? (QuantLib::Real)ValueHelper::Convert (scaleFactor->Value) : 1.5); //4
        _ppFdmBlackScholesMultiStrikeMesher = new boost::shared_ptr<QuantLib::FdmBlackScholesMultiStrikeMesher> (new QuantLib::FdmBlackScholesMultiStrikeMesher ( _size,  _process,  _maturity,  _strikes,  _eps,  _scaleFactor ));
        SetFdm1dMesher (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMultiStrikeMesher));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (strikes != nullptr) strikes->Unlock();    //not optional
    }
}
Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::CFdmBlackScholesMultiStrikeMesher (boost::shared_ptr<QuantLib::FdmBlackScholesMultiStrikeMesher>& childNative, Object^ owner) : CFdm1dMesher(CFdmBlackScholesMultiStrikeMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMultiStrikeMesher = NULL;
#endif
	_ppFdmBlackScholesMultiStrikeMesher = &childNative;
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMultiStrikeMesher));
}
Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::CFdmBlackScholesMultiStrikeMesher (QuantLib::FdmBlackScholesMultiStrikeMesher& childNative, Object^ owner) : CFdm1dMesher(CFdmBlackScholesMultiStrikeMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMultiStrikeMesher = NULL;
#endif
	_ppFdmBlackScholesMultiStrikeMesher = new boost::shared_ptr<QuantLib::FdmBlackScholesMultiStrikeMesher> (&childNative);
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMultiStrikeMesher));
    _FdmBlackScholesMultiStrikeMesherOwner = owner;
    _Fdm1dMesherOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::CFdmBlackScholesMultiStrikeMesher (CFdmBlackScholesMultiStrikeMesher^ copy) : CFdm1dMesher(CFdmBlackScholesMultiStrikeMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMultiStrikeMesher = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmBlackScholesMultiStrikeMesher = new boost::shared_ptr<QuantLib::FdmBlackScholesMultiStrikeMesher> (copy->GetShared());
        _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMultiStrikeMesher));
    }
}
Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::CFdmBlackScholesMultiStrikeMesher (PLATFORM::Type^ t) : CFdm1dMesher(CFdmBlackScholesMultiStrikeMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMultiStrikeMesher = NULL;
#endif
	if (!t->IsSubclassOf(CFdmBlackScholesMultiStrikeMesher::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::CFdmBlackScholesMultiStrikeMesher (QuantLib::Handle<QuantLib::FdmBlackScholesMultiStrikeMesher>& childNative, Object^ owner)  : CFdm1dMesher(CFdmBlackScholesMultiStrikeMesher::typeid)
{
	_phFdmBlackScholesMultiStrikeMesher = &childNative;
	_ppFdmBlackScholesMultiStrikeMesher = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesMultiStrikeMesher>>(childNative.currentLink());
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMultiStrikeMesher));
    _FdmBlackScholesMultiStrikeMesherOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::CFdmBlackScholesMultiStrikeMesher (QuantLib::Handle<QuantLib::FdmBlackScholesMultiStrikeMesher> childNative)  : CFdm1dMesher(CFdmBlackScholesMultiStrikeMesher::typeid)
{
	_phFdmBlackScholesMultiStrikeMesher = &childNative;
	_ppFdmBlackScholesMultiStrikeMesher = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesMultiStrikeMesher>>(childNative.currentLink());
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMultiStrikeMesher));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::CFdmBlackScholesMultiStrikeMesher (QuantLib::FdmBlackScholesMultiStrikeMesher childNative)  : CFdm1dMesher(CFdmBlackScholesMultiStrikeMesher::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesMultiStrikeMesher = NULL;
#endif
	_ppFdmBlackScholesMultiStrikeMesher = new boost::shared_ptr<QuantLib::FdmBlackScholesMultiStrikeMesher> (new QuantLib::FdmBlackScholesMultiStrikeMesher (childNative));
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmBlackScholesMultiStrikeMesher));
}
#endif

Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::~CFdmBlackScholesMultiStrikeMesher ()
{
    if (_ppFdmBlackScholesMultiStrikeMesher != NULL)
    {
	    delete _ppFdmBlackScholesMultiStrikeMesher;
        _ppFdmBlackScholesMultiStrikeMesher = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::!CFdmBlackScholesMultiStrikeMesher ()
{
    if (_ppFdmBlackScholesMultiStrikeMesher != NULL)
    {
	    delete _ppFdmBlackScholesMultiStrikeMesher;
    }
}
QuantLib::FdmBlackScholesMultiStrikeMesher& Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::GetReference ()
{
    if (_ppFdmBlackScholesMultiStrikeMesher == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmBlackScholesMultiStrikeMesher;
}
boost::shared_ptr<QuantLib::FdmBlackScholesMultiStrikeMesher>& Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::GetShared ()
{
    if (_ppFdmBlackScholesMultiStrikeMesher == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmBlackScholesMultiStrikeMesher;
}
QuantLib::FdmBlackScholesMultiStrikeMesher* Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::GetPointer ()
{
    if (_ppFdmBlackScholesMultiStrikeMesher == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmBlackScholesMultiStrikeMesher;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmBlackScholesMultiStrikeMesher>& Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::GetHandle ()
{
	if (_phFdmBlackScholesMultiStrikeMesher == NULL)
	{
		_phFdmBlackScholesMultiStrikeMesher = new Handle<QuantLib::FdmBlackScholesMultiStrikeMesher> (*_ppFdmBlackScholesMultiStrikeMesher);
	}
	return *_phFdmBlackScholesMultiStrikeMesher;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::HasNative () 
{
	return (_ppFdmBlackScholesMultiStrikeMesher != NULL);
}

Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher::ProcessHelper (Cephei::QL::IQuote^ s0, Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Termstructures::IYieldTermStructure^ qTS, Double vol)
{
    CQuote^ _Cs0;
    CYieldTermStructure^ _CrTS;
    CYieldTermStructure^ _CqTS;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cs0 = safe_cast<CQuote^> (s0);
        _Cs0->Lock();
        Handle<QuantLib::Quote>& _s0 = static_cast<Handle<QuantLib::Quote>&> (_Cs0->GetHandle ()); 
        _CrTS = safe_cast<CYieldTermStructure^> (rTS);
        _CrTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _rTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CrTS->GetHandle ()); 
        _CqTS = safe_cast<CYieldTermStructure^> (qTS);
        _CqTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _qTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CqTS->GetHandle ()); 
        QuantLib::Volatility _vol = (QuantLib::Volatility)ValueHelper::Convert (vol); //a
    	boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> _rv = (boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>)(*_ppFdmBlackScholesMultiStrikeMesher)->processHelper ( _s0,  _rTS,  _qTS,  _vol );   
        Cephei::QL::Processes::CGeneralizedBlackScholesProcess^ _nrv = REFNEW Cephei::QL::Processes::CGeneralizedBlackScholesProcess (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cs0 != nullptr) _Cs0->Unlock();
        if (_CrTS != nullptr) _CrTS->Unlock();
        if (_CqTS != nullptr) _CqTS->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Meshers::IFdmBlackScholesMultiStrikeMesher^ Cephei::QL::Methods::Finitedifferences::Meshers::CFdmBlackScholesMultiStrikeMesher_Factory::Create (UInt64 size, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double maturity, Cephei::Core::IVector<Double>^ strikes, Microsoft::FSharp::Core::FSharpOption<Double>^ eps, Microsoft::FSharp::Core::FSharpOption<Double>^ scaleFactor)
{
    return REFNEW CFdmBlackScholesMultiStrikeMesher ( size,  process,  maturity,  strikes,  eps,  scaleFactor);
}
